package cn.edu.nuc.onlinestore.frame;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

import cn.edu.nuc.onlinestore.Person.ObjectStream;
import cn.edu.nuc.onlinestore.model.Goods;
import cn.edu.nuc.onlinestore.network.TCPServer;

import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.io.File;
import java.security.Key;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.awt.event.ActionEvent;

@SuppressWarnings("serial")
public class AdminStore extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
    private String username;
    private JFrame parent;
    private JFrame jf;
	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AdminStore frame = new AdminStore();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
*/
	/**
	 * Create the frame.
	 */
    public AdminStore(){}
	public AdminStore(String username){
		this.username=username;
	}
	
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @wbp.parser.constructor
	 */
	public AdminStore(JFrame frame,String username) {
		this.parent=frame;
		this.jf=this;
		this.username=username;
		setTitle("中北商场后台管理系统--当前用户:"+username);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 732, 467);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(10, 78, 696, 341);
		panel.setLayout(new GridLayout(1, 1, 0, 0));
		
		DefaultTableModel model = new DefaultTableModel();
		model.addColumn("商品编号");
		model.addColumn("名称");
		model.addColumn("单价(人民币)");
		model.addColumn("库存");
		//model.addColumn("操作");
	    //List<Goods> listGoods=client.allGoods();
		
		File dir=new File("D:/store/goods");
		 
		if(!dir.exists()){
			 dir.mkdirs();
		}
		String[] strFile =dir.list();
   
		for (String str:strFile) {
			File GoodsName=new File(dir,str);
			Goods allGoods=ObjectStream.read(Goods.class, "goods/"+GoodsName.getName());
            model.addRow(new String[] { String.valueOf(allGoods.getGid()), allGoods.getGoodsName(),
						String.valueOf(allGoods.getPrice()), String.valueOf(allGoods.getAmount()) });
			}

		
		 
		/*if(!dir.exists()){
			 dir.mkdirs();
		}*/
		
		/*File f=new File("D:/store/goods");
		int FileNum=dir.list().length;
		String path;
		Goods goods;
		if(FileNum!=0){
			for(int i=0;i<FileNum;i++){
		
	     	    path="goods/"+(i+1)+".txt";
	     	    goods=ObjectStream.read(Goods.class, path);
	     		model.addRow(new String[]{String.valueOf(i+1),goods.getGoodsName(),String.valueOf(goods.getPrice()),String.valueOf(goods.getAmount())});
	     	   
			}
		}*/
		JTable table = new JTable( model );
		
		JScrollPane pane = new JScrollPane( table );
		
		panel.add(pane);
		contentPane.add(panel);
		
		JButton button = new JButton("添加商品");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AdminAdd add = new AdminAdd(model);
				add.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
				add.setVisible(true);
				
			}
		});
		button.setBounds(386, 45, 93, 23);
		contentPane.add(button);
		
		JButton button_1 = new JButton("修改商品");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			
				int row=table.getSelectedRow();
				
				if( row == -1 ){
					JOptionPane.showMessageDialog(null,"未选中行！");
				}else{
				    int NO=Integer.parseInt(table.getValueAt(row, 0).toString());
				    
			     	Goods goods=ObjectStream.read(Goods.class, "goods/"+NO);
			     	
			     	String str0=table.getValueAt(row, 0).toString();
				    String str1=table.getValueAt(row, 1).toString();
	                String str2=table.getValueAt(row, 2).toString();
	                String str3=table.getValueAt(row, 3).toString();
	                
					Map<Integer, String> map=new HashMap<Integer, String>();
					map.put(0,str1);
					map.put(1,str2);
					map.put(2,str3);
					map.put(3, goods.getBrief());
					AdminUpdate u = new AdminUpdate(table,row,map,NO);
					u.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
					u.setVisible(true);
				}
			}
		});
		button_1.setBounds(489, 45, 93, 23);
		contentPane.add(button_1);
		
		JButton button_2 = new JButton("删除选中商品");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//得到当前选中商品项
				int selectrow=table.getSelectedRow();
				
				//File file;
                Goods goods=null;
			
					
				if(selectrow!=-1){
					int NO=Integer.parseInt(table.getValueAt(selectrow, 0).toString());
					goods=ObjectStream.read(Goods.class, "/goods/"+NO);
					int remove = JOptionPane.showConfirmDialog(null,"确定删除商品："+goods.getGoodsName(),"不退出",JOptionPane.OK_CANCEL_OPTION);
					if( remove == 0 ){
				
						File f = new File("D:/store/goods/"+NO);
						f.delete();
						model.removeRow(selectrow);
					}
				
				}else{
						JOptionPane.showConfirmDialog(null, "您未选中商品！");
				}
			
				
		    }
		});
		button_2.setBounds(587, 45, 119, 23);
		contentPane.add(button_2);
		
		JButton button_3 = new JButton("退出登录");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				jf.dispose();
				parent.setVisible(true);
			}
		});
		button_3.setBounds(613, 6, 93, 23);
		contentPane.add(button_3);
		
		JLabel label = new JLabel("当前在线用户数: 0");
		label.setBounds(10, 10, 162, 15);
		contentPane.add(label);
		
		JLabel lblid = new JLabel("商品编号:");
		lblid.setBounds(10, 53, 54, 15);
		contentPane.add(lblid);
		
		textField = new JTextField();
		textField.setBounds(68, 50, 66, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JButton button_4 = new JButton("搜索");
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int No=textField.getText().trim().length();
			
				if(No==0){
					File dir=new File("D:/store/goods");
					 
					if(!dir.exists()){
						 dir.mkdirs();
					}
					String[] strFile =dir.list();
				    model.setNumRows(0);
					for (String str:strFile) {
						File GoodsName=new File(dir,str);
						Goods allGoods=ObjectStream.read(Goods.class, "goods/"+GoodsName.getName());
			            model.addRow(new String[] { String.valueOf(allGoods.getGid()), allGoods.getGoodsName(),
									String.valueOf(allGoods.getPrice()), String.valueOf(allGoods.getAmount()) });
						}

				}else{
				
				     for (String string:strFile) {
						 File GoodsName=new File(dir,string);
						//int index=Integer.parseInt(GoodsName.getName().split(".")[0]);
					     if(textField.getText().equals(GoodsName.getName())){
					    	 int index=Integer.parseInt(textField.getText());
						     String  path="goods/"+index;
						     
						     Goods goods=ObjectStream.read(Goods.class, path);
						     
						     model.setNumRows(0);
						     model.addRow(new String[]{String.valueOf(index),goods.getGoodsName(),String.valueOf(goods.getPrice()),String.valueOf(goods.getAmount())});
						     textField.setText("");
						     continue;
					     }else{
					    	//JOptionPane.showMessageDialog(null, "请输入正确编号！");
					    	continue;
					     }
		         
				   }
				}

			}
		});
		button_4.setBounds(150, 49, 119, 23);
		contentPane.add(button_4);
		
		TCPServer server=new TCPServer(label);
		server.setDaemon(true);
		server.start();
	}
}
